package com.rescue.service.impl;

import com.alibaba.fastjson.JSON;
import com.rescue.constant.MessageConstant;
import com.rescue.constant.SysSettingConstant;
import com.rescue.forum.entity.SysSetting;
import com.rescue.forum.syssetting.CommentSet;
import com.rescue.forum.vo.SysSettingVO;
import com.rescue.mapper.SysSettingMapper;
import com.rescue.service.SysSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysSettingServiceImpl implements SysSettingService {

    @Autowired
    private SysSettingMapper sysSettingMapper;

    /**
     * 获取系统设置
     *
     * @return
     */
    public SysSettingVO getVO() {
        List<SysSetting> list = sysSettingMapper.getAll();
        Map<String, String> map = new HashMap<>();
        list.forEach(sysSetting -> map.put(sysSetting.getCode(), sysSetting.getJsonContent()));
        return SysSettingVO.builder()
                .auditSetting(map.get(SysSettingConstant.AUDIT_SETTING))
                .commentSetting(map.get(SysSettingConstant.COMMENT_SETTING))
                .postSetting(map.get(SysSettingConstant.POST_SETTING))
                .likeSetting(map.get(SysSettingConstant.LIKE_SETTING))
                .registerSetting(map.get(SysSettingConstant.REGISTER_SETTING))
                .build();
    }

    //保存系统设置
    public void update(SysSettingVO sysSettingVO) {
        sysSettingMapper.update(new SysSetting(SysSettingConstant.AUDIT_SETTING,sysSettingVO.getAuditSetting()));
        sysSettingMapper.update(new SysSetting(SysSettingConstant.COMMENT_SETTING,sysSettingVO.getCommentSetting()));
        sysSettingMapper.update(new SysSetting(SysSettingConstant.LIKE_SETTING,sysSettingVO.getLikeSetting()));
        sysSettingMapper.update(new SysSetting(SysSettingConstant.POST_SETTING,sysSettingVO.getPostSetting()));
        sysSettingMapper.update(new SysSetting(SysSettingConstant.REGISTER_SETTING,sysSettingVO.getRegisterSetting()));
    }

    /**
     * 查询评论是否开启
     * @return
     */
    public Boolean getSysSettingC() {
        String s = sysSettingMapper.getByCode(SysSettingConstant.COMMENT_SETTING);
        CommentSet commentSet = JSON.parseObject(s, CommentSet.class);
        return commentSet.getCommentOpen();
    }
}
